package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GroupUserListResponse;
import com.wephone.Student.bean.data.GroupInfoData;
import com.wephone.Student.bean.data.GroupUserListData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;

public class GroupDetailActivity extends BaseActivity {
	private GridView gridView;
	private GridViewAdapter adapter;
	public static GroupInfoData groupInfo;
	private TextView groupText1;
	private TextView groupText2;
	private TextView groupText3;
	private TextView groupText4;
	private TextView groupText5;
	private TextView groupText6;
	private TextView groupText7;
	private List<GroupUserListData> groupUserList = new ArrayList<GroupUserListData>();
	private LinearLayout memberLayout;
	private TextView groupMemberCount;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(GroupDetailActivity.this,GroupUserListResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					GroupUserListResponse res = (GroupUserListResponse)response;
					
					groupUserList.addAll(res.getData());
					adapter.notifyDataSetChanged();
					
					memberLayout.removeAllViews();
					for(GroupUserListData userData:groupUserList){
						View subView = getLayoutInflater().inflate(R.layout.include_group_detail_member_view_item, null);
						ImageView itemImage = (ImageView)subView.findViewById(R.id.item_image);
						if(!TextUtils.isEmpty(userData.getuInfo().getuIcon())){
							imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuInfo().getuIcon(), itemImage);
						}
						memberLayout.addView(subView);
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler2(){
		MyResponseHandler handler = new MyResponseHandler(GroupDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					Toast.makeText(getApplicationContext(), "入群申请已提交，请等待审核", Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_detail);
		
		init();
	}
	
	private void init(){
		gridView = (GridView)findViewById(R.id.gridview);
		adapter = new GridViewAdapter();
		gridView.setAdapter(adapter);
		groupText1 = (TextView)findViewById(R.id.user_info_detail_edit_1);
		groupText2 = (TextView)findViewById(R.id.user_info_detail_edit_2);
		groupText3 = (TextView)findViewById(R.id.user_info_detail_edit_3);
		groupText4 = (TextView)findViewById(R.id.user_info_detail_edit_4);
		groupText5 = (TextView)findViewById(R.id.user_info_detail_edit_5);
		groupText6 = (TextView)findViewById(R.id.user_info_detail_edit_6);
		groupText7 = (TextView)findViewById(R.id.user_info_detail_edit_7);
		memberLayout = (LinearLayout)findViewById(R.id.group_detail_member_layout);
		groupMemberCount = (TextView)findViewById(R.id.group_detail_member_count);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		findViewById(R.id.btn_bottom_item_1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showInputAddGroupDialog(getApplicationContext());
			}
		});
		
		groupMemberCount.setText("（"+groupInfo.getgMemberCount()+"/"+groupInfo.getgTotalCount()+"）");
		
		groupText1.setText(groupInfo.getgGroupId());
		groupText2.setText(groupInfo.getgCountry());
		groupText3.setText(groupInfo.getgCity());
		groupText4.setText(Util.gps2km(groupInfo.getgGpsX(), groupInfo.getgGpsY(), ShareReferenceUtil.getLocationLatitude(getApplicationContext()), ShareReferenceUtil.getLocationLontitude(getApplicationContext()))+"km");
		groupText6.setText(groupInfo.getgDetail());
		try {
			Date date = new Date(Long.parseLong(groupInfo.getgTs())*1000);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
			groupText7.setText(sdf2.format(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		RequestParams params = new RequestParams();
		params.put("Page", "1");
		params.put("PageSize", ""+Constants.PAGE_SIZE);
		params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		params.put("gGroupId", groupInfo.getgGroupId());
		HttpClient.getInstance().get(Constants.API_GET_GROUP_USER_LIST_URL, params, getHandler());
		
	}
	
	private void showInputAddGroupDialog(Context context){
		final View inputView = getLayoutInflater().inflate(R.layout.dialog_add_group_view, null);  
		AlertDialog.Builder builder = new AlertDialog.Builder(GroupDetailActivity.this);
		builder.setTitle("加入群组");
		builder.setView(inputView);  
		builder.setPositiveButton("提交", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				EditText contentEdit = (EditText)inputView.findViewById(R.id.dialog_add_group_input);
				String content = contentEdit.getText().toString();
				if(TextUtils.isEmpty(content)){
					Toast.makeText(getApplicationContext(), "入群理由必须填写", Toast.LENGTH_SHORT).show();
				}else{
					RequestParams params = new RequestParams();
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("gGroupId", groupInfo.getgGroupId());
					params.put("guTS", ""+System.currentTimeMillis()/1000);
					params.put("content", content);
					HttpClient.getInstance().get(Constants.API_ADD_GROUP_IN_URL, params, getHandler2());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}
	
	private class GridViewAdapter extends BaseAdapter{		
		@Override
		public int getCount() {
			return groupUserList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View convertView, ViewGroup arg2) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_group_detail_grid_view_item, null);
			}
			GroupUserListData userData = groupUserList.get(arg0);
			ImageView itemImage = (ImageView)convertView.findViewById(R.id.item_image);
			if(!TextUtils.isEmpty(userData.getuInfo().getuIcon())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuInfo().getuIcon(), itemImage);
			}
			
			return convertView;
		}
	}

}
